package com.gxa.scdx.cloud.user.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


//文件上传控制层
//@author 颜可翔

@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation(value = "文件上传")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws FileNotFoundException {
        Map<String, Object> map = new HashMap<>();
        if (file.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "failed");
            return map;
        }

        //保存文件路径
        //String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
        String staticPath = "/usr/local/data";
        String fileName = file.getOriginalFilename();
        String pathName = staticPath + "/jsonfile/" + fileName;

        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(pathName);
            fos.write(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }


}
